<template>
  <div class="first">
    <div class="box1">
      <div  class="alltitle">商品分类统计</div>
      <div id="zhu"  class="m_standard" style="width: 400px;height: 300px"></div>
    </div>
    <div class="medium">
      <div class="head">商品分类统计饼图</div>
      <div id="pie" style="width: 600px;height: 300px"></div>
    </div>
    <div class="boxr">
      <div  class="alltitle">商品分类统计</div>
      <div id="z"  class="m_standard" style="width: 400px;height: 300px"></div>
    </div>
  </div>
</template>
<script>
import * as echarts from 'echarts';
import {count} from "@/api/demo/goods";

export default {
  data() {
    return {
      listX:[],//X轴的数据
      listY: [], //Y轴的数据
      goodsType:[],
    }
  },
  created() {
    this.getGoodsDto();
  },
  // 基于准备好的dom，初始化echarts实例
  mounted() { //最早获取DOM元素的生命周期函数
  },
  methods:{
    /** 获取商品各类型的数据 */
    getGoodsDto() {
      count().then(response => {
        console.info("rows",response.rows);
        this.goodsType = response.rows;
        this.goodsType.forEach(ele =>{
          this.listX.push(ele.name);
          this.listY.push(ele.value);
        });
        console.log("x",this.listX)
        console.log("y",this.listY)
        this.zhu()
        this.pie()
      });
    },
    //柱状图图
    zhu(){
      var myChart = echarts.init(document.getElementById('zhu'));
// 绘制图表
      myChart.setOption({
        title: {
          text: '商品分类柱状图'
        },
        tooltip: {
          trigger:'axis',
        },
        legend: {
        },
        //下载图片
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          data: this.listX
        },
        yAxis: {},
        series: [
          //折线图
          {
            name: '商品分类',
            type: 'line',
            data: this.listY,
            smooth:true //是否光滑曲线
          }
          //柱状图
          // {
          //   name: '销量',
          //   type: 'bar',
          //   data: [5, 20, 36, 10, 10, 20],
          // }
        ]
      });
    },
    //圆饼图
    pie(){
      var myChart = echarts.init(document.getElementById('pie'));
      var option;

      option = {
        title: {
          text: '商品分类圆饼图',
          subtext: '副标题',
          left: 'center' //居左
        },
        //下载图片
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical', //系列名称
          left: 'left'
        },
        series: [
          {
            name: '商品比例',
            type: 'pie',
            radius: '50%',
            data:this.goodsType,
            //高亮配置
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      };

      option && myChart.setOption(option);
    }
  }
}
</script>
<style>
.first {
  /* 		 background-image: url("../assets/images/login-background.jpg"); */
  display: flex;
  align-items: flex-end;
  margin-top: 8px;
}

.boxr{
  width: 420px;
  height: 355px;
  border: 1px solid #dbdbdb;
  border-radius: 5px;
  /* background-color: cornflowerblue; */
  margin-left: 7px;

}
accessKeyId

.m_standard {
  margin-top: 20px;
  margin-right: 15px;
  margin-bottom: 5px;
}




h1 {
  text-align: center;
  margin-bottom: 50px;
}

</style>
